package org.flyby.api.persistence;

import javax.persistence.*;

@Entity
@Table(name = "CART")
@NamedQueries({
        @NamedQuery(name = "Cart.updateCartTotalPriceAndTotalProductsByProductOrderDetails",
                query = " update Cart c " +
                        " set c.totalPrice = (:orderPrice + c.totalPrice)," +
                        "     c.totalProducts = (:units + c.totalProducts), " +
                        "     c.totalWeight = (:orderWeight + c.totalWeight) " +
                        " where c.id= :cartId ")
})
public class Cart implements BaseEntity {

    @Id
    @Access(AccessType.PROPERTY)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "CART_ID", nullable = false)
    private int id;

    @Column(name = "TOTAL_PRICE", nullable = false)
    private Float totalPrice;

    @Column(name = "TOTAL_WEIGHT", nullable = false)
    private Float totalWeight;

    @Column(name = "TOTAL_PRODUCT", nullable = false)
    private Integer totalProducts;

    @Column(name = "CART_BARCODE", nullable = true)
    private String cartBarcode;

    @ManyToOne(optional = false)
    @JoinColumn(name = "USER_ID", referencedColumnName = "USER_ID", nullable = false)
    private User user;

    @ManyToOne(optional = false, fetch = FetchType.EAGER)
    @JoinColumn(name = "CART_STATUS_TB", referencedColumnName = "CODE_TABLE_DATA_ID", nullable = false)
    private CodeTableData cartStatus;

    public Cart() {
        super();
    }

    @Override
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Float getTotalPrice() {
        return totalPrice;
    }

    public void setTotalPrice(Float totalPrice) {
        this.totalPrice = totalPrice;
    }

    public Float getTotalWeight() {
        return totalWeight;
    }

    public Float getTotalWeightKg() {
        return totalWeight / 1000;
    }

    public void setTotalWeight(Float totalWeight) {
        this.totalWeight = totalWeight;
    }

    public Integer getTotalProducts() {
        return totalProducts;
    }

    public void setTotalProducts(Integer totalProduct) {
        this.totalProducts = totalProduct;
    }

    public String getCartBarcode() {
        return cartBarcode;
    }

    public void setCartBarcode(String cartBarcode) {
        this.cartBarcode = cartBarcode;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }


    public CodeTableData getCartStatus() {
        return cartStatus;
    }

    public void setCartStatus(CodeTableData cartStatus) {
        this.cartStatus = cartStatus;
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Cart cart = (Cart) o;

        if (id != cart.id) return false;
        if (user != null ? !user.equals(cart.user) : cart.user != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = id;
        result = 31 * result + (user != null ? user.hashCode() : 0);
        return result;
    }
}
